// 初始化数组
var nums = new Array(5).fill(0)
console.log(nums);
var numsList = [0, 1, 2, 3, 4]
console.log(numsList)
// 元素的内存地址 = 数据的内存地址(首元素的地址) + 元素长度 * 元素索引 (地址偏移量)

// 随机访问元素
function randomAccess (numsList) {
  let random_index = Math.floor(Math.random() * numsList.length)
  let random_num = numsList[random_index]
  return random_num
}
console.log(randomAccess(numsList))

function insert (list, nums, index) {
  for (let i = list.length - 1; i > index; i--) {
    list[i] = list[i - 1]
  }
  list[index] = nums
  return list
}
// console.log(insert(numsList, 2.5, 3));
// console.log(numsList);

function remove (list, index) {
  for (let i = index; i < list.length - 1; i++) {
    list[i] = list[i + 1]
  }
  return list
}
// console.log(remove(numsList, 3));

function tranverse (nums) {
  let count = 0
  for (let i = 0; i < nums.length; i++) {
    count++
  }
  for (const num of nums) {
    count += 1
  }
  console.log(count);
}
console.log(tranverse(numsList));

function find (nums, target) {
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] === target) return i
  }
  return -1
}
console.log(find(numsList, 2))
console.log(find(numsList, 4))
console.log(find(numsList, 7))